<?php

namespace App\Http\Controllers\Admin;

use App\Models\Companyplan;
use App\Models\Order;
use App\Models\Store;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CompanyplanController extends Controller
{
    public function __construct()
    {
        view()->share([
            '_orders' => 'am-in',
            '_companyplan' => 'am-active',

        ]);

    }

    //公司管理首页
    public function index(Request $request)
    {

        $where = function ($query) use ($request) {
             //按公司名
            if ($request->has('name') and $request->name != '') {
                $search = "%" . $request->name . "%";
                $query->where('name', 'like', $search);
            }
            //按联系人电话
            if ($request->has('tel') and $request->tel != '') {
                $search = "%" . $request->oncustomer_tel . "%";
                $query->where('tel', 'like', $search);
            }
            //按创建时间
            if ($request->has('created_at') and $request->created_at != "") {
                $time = explode(" ~ ", $request->created_at);
                $start = $time[0] . ' 00:00:00';
                $end = $time[1] . ' 23:59:59';
                $query->whereBetween('created_at', [$start, $end]);
            }
            //按门店
            if ($request->has('store_id') and $request->store_id != '-1') {

                $query->where('store_id', $request->store_id);
            }


        };

        //按门店
        $cates = Store::all();
        //所有公司，关联门店
        $items = Companyplan::where($where)->with('store')->orderBy('created_at', 'desc')->paginate(10);
        return view('admin.order.companyplan', compact('items', 'cates'));
    }


}
